<title>新的文章</title>
<div class="layui-card layadmin-header">
<div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>应用</cite></a>
    <a><cite>新的文章</cite></a>
</div>
</div>
<div class="layui-fluid">
    <div class="layui-form" lay-filter="change-article">
        <div class="layui-form-item">
            <input type="text" name="title" lay-verify="required" placeholder="标题" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-item">
            <input type="text" name="foreword" lay-verify="required" placeholder="引言" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-item">
            <div name="item" id="editor">
                <p>好久不见</p>
            </div>
        </div>
        <div class="layui-form-item">
            <select name="class_id" lay-verify="required" lay-filter="classify" id="classify_list">
                
            </select>
        </div>
        <div class="layui-form-item">
            <div style="margin-left: -1px;" class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="push">发布</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </div>
</div>
<script src="../../src/asset/wangEditor.min.js"></script>
<script>
    layui.use(['admin','server','form','laytpl'],function(){
        var $ = layui.$;
        var form = layui.form;
        var setter = layui.setter;
        var laytpl = layui.laytpl;
        var server = layui.server;
        var admin = layui.admin;
        var getTpl = classTpl.innerHTML;
        var article = layui.sessionData('changeTable').changeArticle;

        //  渲染分类
        var class_options = {};
        if(article){
            class_options.async = true;
        }
        class_options.url = 'getAllClassify';
        class_options.callback = function(data){
            var view = document.getElementById('classify_list');
            laytpl(getTpl).render(data.data, function(html){
                view.innerHTML= html;
                if(article){
                    form.val('change-article',article.data[0]);
                }
                form.render();
            });
        };
        server.req(class_options);

        
        var E = window.wangEditor;
        var editor = new E('#editor');
        if(article){
            $("#editor").html(article.data[0].content);
            layui.sessionData('changeTable',{
                key:'changeArticle',
                remove:true
            });
        }else{
            var lastText = layui.data(setter.tableName).saveText;
            if(lastText){
                $("#editor").html(lastText);
            }
        }
        //  设置上传文件接口
        editor.customConfig.uploadImgServer = 'https://www.hanlingsha.love/api/upload';
        //  设置文件名
        editor.customConfig.uploadFileName = 'img';
        //  设置携带参数
        editor.customConfig.uploadImgParams = {
            api_token: layui.data(setter.tableName).api_token,
        };
        editor.customConfig.colors = [
            '#000000',
            '#EE0000',
            '#00FFFF',
            '#FFFF00',
            '#FF83FA',
            '#02F78E',
            '#FF8000',
            '#006000',
            '#EEECE0',
            '#FFFAF0',
            '#FFE4E1',
            '#33FFFF',
        ]
        editor.create();
        form.on('submit(push)',function(data){
            var userInfo = admin.getUserInfo();
            var addArticle_options = {};
            var id = null;
            addArticle_options.url = 'addArticle';
            if(article){
                addArticle_options.url = 'changeArticle';
                id = article.data[0].id;
            }
            addArticle_options.data = {
                id:id,
                author_id:userInfo.id,
                title:data.field.title,
                class_id:data.field.class_id,
                content:editor.txt.html(),
                foreword:data.field.foreword,
            };
            addArticle_options.method = 'post',
            addArticle_options.callback = function(data){
                layui.data(setter.tableName,{key:'saveText', remove:true});
                layer.msg('发布成功',{time:1000},function(){
                    location.reload();
                });
            }
            server.req(addArticle_options);
        });

        $(document).keydown(function(data){
            layui.data(setter.tableName,{
                key:'saveText',
                value: editor.txt.html(), 
            })
        });
    })
</script>
<script type="text/html" id="classTpl">
    <option value="">选个分类吧</option>
    {{#  layui.each(d, function(index, item){ }}
        {{#  if(item.parent_id != 0){ }}
            <option value="{{item.id}}">{{item.name}}</option>
        {{# } }}
    {{#  }); }}
</script>